package com.todo.strategy.pipeline.application.handler.bizAction;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.exception.BizException;
import com.todo.strategy.pipeline.application.entity.context.StrategyActionContext;
import com.todo.strategy.pipeline.common.enums.StrategyActionEnum;
import lombok.extern.slf4j.Slf4j;

/**
 * @author azhebuxing
 * @date 2025/3/1 05:28
 * @description
 */
@Slf4j
public abstract class AbstractStrategyActionHandle {

    public Boolean actionHandle(StrategyActionContext context) {
        log.info("AbstractStrategyActionHandle handle start StrategyActionContext: 【{}】", JSONObject.toJSONString(context));
        if (match(context)) {
            try {
                Boolean result = doActionHandle(context);
                log.info("AbstractStrategyActionHandle handle end action:【{}】 result: 【{}】", getStrategyActionEnum(), result);
            } catch (BizException ex) {
                log.error("AbstractStrategyActionHandle handle bizError action:【{}】 context: 【{}】", getStrategyActionEnum(), JSONObject.toJSONString(context), ex);
                throw ex;
            } catch (Exception ex) {
                log.error("AbstractStrategyActionHandle handle error action:【{}】 context: 【{}】", getStrategyActionEnum(), JSONObject.toJSONString(context), ex);
            }
        }
        return true;
    }

    protected abstract Boolean doActionHandle(StrategyActionContext context);

    protected abstract Boolean match(StrategyActionContext context);

    protected abstract StrategyActionEnum getStrategyActionEnum();
}
